﻿using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Sys.Core.Providers;
using MiniFox;
using MiniFox.Components;
using MiniFox.Configurations;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Platform.Exceptions;
using MiniFox.Security.Text;

namespace Sys.Core.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class UserService : ApiServiceBase, IUserService
    {
        readonly string initPwd;
        public UserService(SystemPrincipal principal) : base(principal)
        {
            var settings = ConfigurationFactory.CommonSettings;
            if (settings.ContainsKey("initPwd"))
                initPwd = settings["initPwd"];
            if (string.IsNullOrEmpty(initPwd))
                initPwd = "666666";
        }
        [AutoWired]
        public IUserProvider Provider { get; set; }

        public UserVo Item(string userId)
        {
            var user = this.Provider.GetUser(userId);
            user.Roles = this.Provider.GetUserRoles(userId);
            return user;
        }

        public ResultObject<IEnumerable<UserVo>> Query(PagedQueryObject<UserQuery> request)
        {
            return this.Provider.Query(request);
        }

        public void ResetPwd(string userId)
        {
            Cipher pwd = this.CreateCipher(this.initPwd);
            if (!this.Provider.ChangePassword(userId, pwd))
                throw new PlatformException("ERR_ChangePwdFail");
        }

        public UserVo Create(UserDto user)
        {
            user.UserID = null;
            if (this.Provider.IsUsedLoginName(user.LoginName))
            {
                throw new PlatformException("ERR_LoginNameExists");
            }
            else if (this.Provider.IsUsedSecEmail(user.SecEmail))
            {
                throw new PlatformException("ERR_SecmailExists");
            }
            else if (this.Provider.IsUsedCellphone(user.Cellphone))
            {
                throw new PlatformException("ERR_CellphoneExists");
            }
            else
            {
                user.UserID = SequenceGUID.NewGUID();
                Cipher pwd = this.CreateCipher(this.initPwd); 
                if (!Provider.CreateUser(user, pwd))
                    return null;

                return this.Item(user.UserID);
            }
        }

        public UserVo Update(UserDto user)
        {
            if (this.Provider.IsUsedLoginName(user.LoginName, user.UserID))
            {
                throw new PlatformException("ERR_LoginNameExists");
            }
            else if (this.Provider.IsUsedSecEmail(user.SecEmail, user.UserID))
            {
                throw new PlatformException("ERR_SecmailExists");
            }
            else if (this.Provider.IsUsedCellphone(user.Cellphone, user.UserID))
            {
                throw new PlatformException("ERR_CellphoneExists");
            }
            if (!Provider.UpdateUser(user))
                return null;

            return this.Item(user.UserID);
        }

        public virtual Cipher CreateCipher(string password)
        {
            MD5HexCipher cipher = password;
            return cipher;
        }
    }
}
